package com.fanbuchi.fukua.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.fanbuchi.fukua.R;
import com.fanbuchi.fukua.activity.base.BaseFragment;
import com.fanbuchi.fukua.adapter.MsgAdapter;
import com.fanbuchi.fukua.animator.SlideInLeftAnimator;
import com.fanbuchi.fukua.api.MsgesAPI;
import com.fanbuchi.fukua.entity.MsgEntity;
import com.fanbuchi.fukua.entity.enums.Actions;
import com.fanbuchi.fukua.entity.event.ClickEvent;
import com.fanbuchi.fukua.entity.event.LongClickEvent;
import com.fanbuchi.fukua.net.BaseCallBack;
import com.fanbuchi.fukua.widget.DividerItemDecoration;
import com.fanbuchi.fukua.widget.swipy.SwipyRefreshLayout;
import com.fanbuchi.fukua.widget.swipy.SwipyRefreshLayoutDirection;
import com.squareup.okhttp.Callback;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

/**
 * Created by rabbit on 2015/6/6.
 */
public class HomeContentFragment extends BaseFragment implements SwipyRefreshLayout.OnRefreshListener{

    private static final String ARG_SECTION_NUMBER = "section_number";
    private String mTitle;
    @InjectView(R.id.swipyrefreshlayout)
    SwipyRefreshLayout mSwipyRefreshLayout;
    @InjectView(R.id.recyclerView)
    android.support.v7.widget.RecyclerView mRecyclerView;

    MsgAdapter msgAdapter;


    List<MsgEntity> msgEntities = new ArrayList<>();
    LinearLayoutManager manager = new LinearLayoutManager(getActivity());

    public HomeContentFragment() {
        EventBus.getDefault().register(this);
    }

    public static HomeContentFragment newInstance(int sectionNumber) {
        HomeContentFragment fragment = new HomeContentFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    protected void setupViews() {
        //config
        manager.setOrientation(LinearLayout.VERTICAL);

        manager.scrollToPosition(0);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setHasFixedSize(true);
        // divider item
        DividerItemDecoration itemDecoration =
                new DividerItemDecoration(getActivity(),
                        DividerItemDecoration.VERTICAL_LIST);
        mRecyclerView.addItemDecoration(itemDecoration);
        mRecyclerView.setItemAnimator(new SlideInLeftAnimator());

        //recyclerView.addOnItemTouchListener(this);
        msgAdapter = new MsgAdapter(msgEntities);
        mRecyclerView.setAdapter(msgAdapter);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_bottom_top_refresh, container, false);

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //  ((HomeActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.inject(this, view);
        setupViews();
        mSwipyRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //section_label.setText(getResources().getStringArray(R.array.navigation_title)[getArguments().getInt(ARG_SECTION_NUMBER)-1]);
       // getMsgesByPage(page, page_size);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onRefresh(SwipyRefreshLayoutDirection direction) {
        if (direction==SwipyRefreshLayoutDirection.TOP){
            page=1;
            next_page=page+1;
            getMsgesByPage(page,page_size);
        }else{
            getMsgesByPage(next_page,page_size);
        }

    }




    public  void getMsgesByPage(int page,int page_size){
        try {
            MsgesAPI.getAPI().getMsgsByPage(page,page_size, getMsgesByPageCallBack());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected Callback getMsgesByPageCallBack(){
        mSwipyRefreshLayout.setRefreshing(false);
        Callback _callBack=  new BaseCallBack<MsgEntity[]>(MsgEntity[].class) {
            @Override
            public void onResponseOk(MsgEntity[] body) {

                if (page==1){
                    msgEntities.clear();
                    msgEntities.addAll(Arrays.asList(body));
                }else{
                    msgEntities.addAll(Arrays.asList(body));
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        msgAdapter.notifyDataSetChanged();
                        manager.scrollToPosition(msgEntities.size()-page_size);
                    }
                });
                page=next_page;
            }
        };
        return _callBack;
    }


    public void onEvent(ClickEvent<MsgEntity> clickEvent){
        if (clickEvent.getAction()== Actions.action_click){
            Intent intent = new Intent(getActivity(),MsgActivity.class);
            intent.putExtra(mIdata,clickEvent.getData());
            startActivity(intent);
        }
    }


    public void onEvent(LongClickEvent<MsgEntity> longClickEvent){
        if (longClickEvent.getAction()==Actions.action_longclick){

        }
    }

}
